<?php
/**
 * $Id: msys-exception.php 28 2011-11-23 04:58:18Z nhnoah $;
 * msys-exception.php, class error handler
 * 
 * Base class for all msysExceptions. 
 *  
 * ================================================================
 * @package     msys-file-manager
 * @category    exception
 * ================================================================
 * 
 * @author      nhnoah <nhnoah+msys@gmail.com> 
 * @copyright   Copyright (c) 2011, Nuar H. Noah (nhnoah) 
 * @license     http://www.gnu.org/licenses/lgpl.html GNU Lesser GPL
 * @link        http://msys-file-manager.googlecode.com/
 * 
 * ================================================================
 * SVN revision information:
 * 
 * @version $Revision: 28 $:
 * @author  $Author: nhnoah $:
 * @date    $Date: 2011-11-23 04:58:18 +0000 (Wed, 23 Nov 2011) $:
 * @source	$URL: http://msys-file-manager.googlecode.com/svn/branches/msys/matrix-sys/class/msys-exception.php $: 
 * ================================================================
 * 
 * @since   0.3.1.27 (2011-11-22 16:39:24Z)
 */

/**
 * msysException Interface
 * 
 * @package msys-file-manager   
 * @subpackage exception
 * 
 * @link http://www.php.net/manual/en/language.exceptions.php#91159
 * 
 * @author ask <ask@nilpo.com>
 * @version $Id: msys-exception.php 28 2011-11-23 04:58:18Z nhnoah $:
 * @access public
 */
interface IException
{
    /* Protected methods inherited from Exception class */
    /**
     * getMessage()
     * 
     * @return
     */
    public function getMessage();                 // Exception message 
    /**
     * getCode()
     * 
     * @return
     */
    public function getCode();                    // User-defined Exception code
    /**
     * getFile()
     * 
     * @return
     */
    public function getFile();                    // Source filename
    /**
     * getLine()
     * 
     * @return
     */
    public function getLine();                    // Source line
    /**
     * getTrace()
     * 
     * @return
     */
    public function getTrace();                   // An array of the backtrace()
    /**
     * getTraceAsString()
     * 
     * @return
     */
    public function getTraceAsString();           // Formated string of trace
    
    /* Overrideable methods inherited from Exception class */
    /**
     * __toString()
     * 
     * @return
     */
    public function __toString();                 // formated string for display
    /**
     * __construct()
     * 
     * @param mixed $message
     * @param integer $code
     * @return
     */
    public function __construct($message = null, $code = 0);
}

/**
 * abstract class for custom exception
 * 
 * @package msys-file-manager   
 * @subpackage exception
 * 
 * @link http://www.php.net/manual/en/language.exceptions.php#91159
 * 
 * @author ask <ask@nilpo.com>
 * @since r27
 * @access public
 */
abstract class msysAException extends Exception implements IException
{
    protected $message = 'Unknown exception';     // Exception message
    private   $string;                            // Unknown
    protected $code    = 0;                       // User-defined exception code
    protected $file;                              // Source filename of exception
    protected $line;                              // Source line of exception
    private   $trace;                             // Unknown

    /**
     * msysAException::__construct()
     * 
     * @param mixed $message
     * @param integer $code
     * @return
     */
    public function __construct($message = null, $code = 0)
    {
        if (!$message) {
            throw new $this('Unknown '. get_class($this));
        }
        parent::__construct($message, $code);
    }
    
    /**
     * msysAException::__toString()
     * 
     * @return
     */
    public function __toString()
    {
        return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})".PHP_EOL."{$this->getTraceAsString()}";
    }
}

/**
 * msysException
 * 
 * @package msys-file-manager
 * @subpackage exception   
 * @author nhnoah <nhnoah+msys@gmail.com>
 * @copyright Copyright (c) 2011, Nuar H. Noah (nhnoah) 
 * @since r27
 * @access public
 */
class msysException extends msysAException {
    
    const C_FILE_NOT_FOUND = 404;
    
    const C_INVALID_TYPE = 500;
    
    const WIKI_TAG = '/\[wiki\](.*?)\[\/wiki\]/is';
    
    /**
     * msysException::showException()
     * 
     * @return void
     */
    public function showException(){
        echo '<div class="exception"><h2>Caught Exception</h2>';
        echo '<dl><dt>Err code :</dt><dd>'.$this->getCode().'</dd>';
        echo '<dt>On line :</dt><dd>'.$this->getLine().'</dd><dt>Filename :</dt><dd>'.$this->getFile().'</dd>';
        echo '<dt>Message :</dt>';
        echo '<dd>'.$this->_wikified($this->getMessage()).'</dd>';
        echo '<fieldset><legend>Backtrace summary</legend>';
        echo '<pre>'.$this->getTraceAsString().'</pre></fieldset>';
        echo '</div>';      
    }
    
    /**
     * msysException::_wikified()
     * 
     * @param mixed $message
     * @return
     */
    private function _wikified($message)
    {        
        return preg_replace(self::WIKI_TAG,
            '<a href="'.get_base_uri('ErrorCode#Error_code_reference','wiki').'">$1</a>',
            $message);
    }    
}
?>